<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Array map</title>
</head>
<body>

    <script>

        

        // 1. 案例一：将字符串数组转为数值数组
        var arr = ['1', '3', 'a', '50', ' 31', '-5'];
        var newArr = arr.map((x) => {
            return parseInt(x);
        })
        console.log(newArr)

        // 2. 案例二：重新组织对象
        var arr = [
            {
                id: '1',
                name: 'Jack'
            },
            {
                id: '2',
                name: 'Rose'
            },
            {
                id: '3',
                name: 'Linda'
            }
        ]
        var newArr = arr.map((obj) => {
            var newObj = {}
            newObj[obj.id] = obj.name
            return newObj;
        })
        console.log(arr)
        console.log(newArr)

        // 手动实现map
        // Array.prototype.map = function(callback) {
        //     if (typeof callback !== 'function') {
        //         throw new TypeError(callback + 'is not a function');
        //     }
        //     var arr = []
        //     this.forEach(function(x, index) {
        //         arr[index] = callback(x);
        //     })
        //     return arr;
        // }
    </script>
    
</body>
</html>